package kafka.mq.util;

import java.io.IOException;
import java.text.SimpleDateFormat;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonUtils {
	private static Logger logger = LoggerFactory.getLogger(JsonUtils.class);
	private static ObjectMapper objectMapper = new ObjectMapper();
	static {
		objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); 
		objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
		objectMapper.getDeserializationConfig().withDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
		objectMapper.getSerializationConfig().withDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
		objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
		objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
	}


	public static <T> String toJson(T t) {
		try {
			String jsonStr = objectMapper.writeValueAsString(t);
			return jsonStr;
		} catch (JsonGenerationException e) {
			logger.error("JsonGenerationException",t, e);
		} catch (JsonMappingException e) {
			logger.error("JsonMappingException", e);
		} catch (IOException e) {
			logger.error("IOException", e);
		}
		return null;
	}

	public static <T> T fromJSON(String jsonString, Class<T> clazz) {

		T object = null;
		try {
			object = objectMapper.readValue(jsonString, clazz);
		} catch (JsonGenerationException e) {
			logger.error("JsonGenerationException: {}",jsonString, e);
		} catch (JsonMappingException e) {
			logger.error("JsonMappingException: {}",jsonString, e);
		} catch (IOException e) {
			logger.error("IOException: {}",jsonString, e);
		}
		return object;
	}

	public static <T> T fromJSON(String jsonString, TypeReference<T> typeReference) {

		T object = null;
		try {
			object = objectMapper.readValue(jsonString, typeReference);
		} catch (JsonGenerationException e) {
			logger.error("JsonGenerationException: {}",jsonString, e);
		} catch (JsonMappingException e) {
			logger.error("JsonMappingException: {}",jsonString, e);
		} catch (IOException e) {
			logger.error("IOException: {}",jsonString, e);
		}
		return object;
	}
	
	/**
	 * 格式化输出，性能有损耗
	 */
	public static <T> String toJsonForDisplay(T t) {
		try {
			String jsonStr = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(t);
			return jsonStr;
		} catch (JsonGenerationException e) {
			logger.error("JsonGenerationException", e);
		} catch (JsonMappingException e) {
			logger.error("JsonMappingException", e);
		} catch (IOException e) {
			logger.error("IOException", e);
		}
		return null;
	}


}
